﻿using BLL;
using ChamCongMain.Form;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

namespace ChamCongMain.UserControl
{
    public partial class uclDatHang : DevExpress.XtraEditors.XtraUserControl
    {
        DatHangBLL dhbll = new DatHangBLL();
        int s = 0;
        public uclDatHang()
        {
            InitializeComponent();
            gridDatHang.DataSource = dhbll.GetAllDatHang();
        }

        private void RefeshG(object formName)
        {
            gridDatHang.DataSource = formName;
        }
        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmDatHang frm = new frmDatHang(ChucNang.Them, null);
            frm.LoadData = new BLL.Delegates.RefeshGrid(RefeshG);
            frm.ShowDialog();
            this.Cursor = Cursors.Default;
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            s = gvDatHang.GetDataSourceRowIndex(gvDatHang.FocusedRowHandle);
            if (gvDatHang.RowCount == 0)
            {
                XtraMessageBox.Show("Không có dữ liệu để sửa !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Cursor = Cursors.Default;
                return;
            }
            else
            {
                List<DatHang> listdh = (List<DatHang>)gvDatHang.DataSource;
                DatHang dh = (DatHang)listdh[gvDatHang.FocusedRowHandle];
                frmDatHang frm = new frmDatHang(ChucNang.Sua, dh);
                frm.LoadData = new Delegates.RefeshGrid(RefeshG);
                frm.ShowDialog();
            }
            gvDatHang.MoveBy(s);
            this.Cursor = Cursors.Default;
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            DatHang dh = (DatHang)gvDatHang.GetRow(gvDatHang.FocusedRowHandle);
            if (dh == null)
            {
                this.Cursor = Cursors.Default;
                return;
            }
            else
            {
                try
                {
                    dhbll.DeleteDatHang(dh);
                    XtraMessageBox.Show("Xoá thành công!", "Xóa");
                    gvDatHang.DeleteSelectedRows();
                    gridDatHang.DataSource = dhbll.GetAllDatHang();
                }
                catch
                {
                    XtraMessageBox.Show("Trùng khóa ngoại không xóa được!!!", "Error");
                }
            }
            this.Cursor = Cursors.Default;
        }

        private void btnXuatExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
            if (gvDatHang.RowCount == 0)
            {
                XtraMessageBox.Show("Không có dữ liệu để xuất file !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Cursor = System.Windows.Forms.Cursors.Default;
                return;
            }
            DevExpress.XtraGrid.Views.Grid.GridView grid = gridDatHang.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
            if (grid != null)
            {
                SaveFileDialog saveExcel = new SaveFileDialog();
                saveExcel.Filter = "Excel Workbook|*.xlsx|Excel Macro-Enabled Workbook|*.xlsm|Excel 97-2003 Workbook|*.xls";// định giạng file lưu
                saveExcel.Title = "Save an Excel File";
                saveExcel.InitialDirectory = @"C:\";
                if (saveExcel.ShowDialog() == DialogResult.OK)
                {
                    grid.ExportToXlsx(saveExcel.FileName);
                    XtraMessageBox.Show("Export File thành công!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (XtraMessageBox.Show("Bạn có muốn xem file ?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        Process exportXlsx = new Process();
                        exportXlsx.StartInfo.FileName = "EXCEL.exe";
                        exportXlsx.StartInfo.Arguments = saveExcel.FileName;
                        exportXlsx.Start();
                    }
                }
            }
            this.Cursor = Cursors.Default;
        }

        private void gvDatHang_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
        }

        private void btnTimKiem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            string kytu = string.Empty;
            kytu = txtSearch.EditValue.ToString();
            gridDatHang.DataSource = dhbll.SearchByTen(kytu);
            this.Cursor = Cursors.Default;
        }
    }
}
